package com.four.bmm.service;

import com.four.bmm.dao.INewsDao;
import com.four.bmm.entity.News;
import com.four.bmm.entity.Page;
import com.four.bmm.util.PageUtil;
import com.four.bmm.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class NewsService {
    //注意session关闭的位置

    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    INewsDao newsDao = session.getMapper(INewsDao.class);

    /**
     * 查询所有新闻
     *
     * @return
     */
    public List<News> GetAllNews(int pageno , String keyword){
        Page page = new Page();
        page.setPageStart(PageUtil.PAGE_SIZE * (pageno - 1));
        page.setPageSize(PageUtil.PAGE_SIZE);
        page.setKeyword(keyword);
        List<News> newsList = newsDao.GetAllNews(page);
        return newsList;
    }

    /**
     * 新闻总条数
     *
     * @return
     */
    public int NewsCount(String keyword) {
        Page page = new Page();
        page.setKeyword(keyword);
        int flag = newsDao.NewsCount(page);
        return flag;
    }

    /**
     * 增加新闻
     *
     * @param news
     * @return
     */
    public boolean setNews(News news) {
        boolean flag = false;
        try {
            flag = newsDao.setNews(news);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.close();
        }
        return flag;
    }

    /**
     * 删除新闻
     *
     * @param newsid
     * @return
     */
    public boolean delNews(int newsid) {
        boolean flag = false;
        try {
            flag = newsDao.delNews(newsid);
            session.commit();
        } catch (Exception e) {

            session.close();
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改新闻
     *
     * @param news
     * @return
     */
    public boolean updNews(News news) {
        boolean flag = false;
        try {
            flag = newsDao.updNews(news);
            session.commit();
        } catch (Exception e) {

            session.close();
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * MSJ
     * 增加浏览量
     */
    public int addSeeCount(int newsid) {
        int num = -1;
        try {
            num = newsDao.addSeeCount(newsid);
            session.commit();
        } catch (Exception e) {

            session.close();
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 增加点赞量
     */
    public int addClickCount(int newsid) {
        int num = -1;
        try {
            num = newsDao.addClickCount(newsid);
            session.commit();
        } catch (Exception e) {
            session.close();
        }
        return num;
    }

    /**
     * 根据id查询
     *
     * @param newsid
     * @return
     */
    public News getNewsById(int newsid) {
        return newsDao.getNewsById(newsid);
    }

    /**
     * 减少点赞量
     *
     * @param newsid
     * @return
     */
    public int minusClickCount(int newsid) {
        int num = -1;
        try {
            num = newsDao.minusClickCount(newsid);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.close();
        }
        return num;
    }

    /**
     * 无分页查询所有，
     *
     * @return
     */
    public List<News> GetAllNewsNoPage() {
        return newsDao.GetAllNewsNoPage();
    }

}
